package com.tanhua.dubbo.api;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.tanhua.dubbo.mappers.SettingsMapper;
import com.tanhua.dubbo.mappers.UserMapper;
import com.tanhua.model.domain.Settings;
import com.tanhua.model.domain.User;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
@DubboService
public class SettingsImpl implements SettingsApi{
    @Autowired
    private SettingsMapper settingsMapper;
    @Autowired
    private UserMapper userMapper;
    @Override
    public Settings findByUserid(Long userId) {
        LambdaQueryWrapper<Settings> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Settings::getUserId,userId);
        return settingsMapper.selectOne(queryWrapper);
    }

    @Override
    public void save(Settings settings) {
        settingsMapper.insert(settings);
    }

    @Override
    public void update(Settings settings) {
        settingsMapper.updateById(settings);
    }

    @Override
    public Boolean getPhone(String phone) {
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(User::getMobile,phone);
        Integer count = userMapper.selectCount(queryWrapper);
        return count==0;
    }

    @Override
    public void updatePhone(Long userId, String phone) {
        User user = new User();
        user.setMobile(phone);
        user.setId(userId);
        userMapper.updateById(user);
    }
}
